<?php
namespace Common\Util;

use Services\Article\Exceptions\ArticleException;
use Services\Upload\OssFileService;

class CmsData{

	/**
	 * 获取轮播图
	 *
	 * @param int   $cate_id
	 * @param int   $num
	 * @param array $map
	 * @return null
	 */
	public static function getAdList($cate_id = 0, $num = 10, $map = []){
		$t_cms_ad = M("cms_ad");
		if($cate_id > 0){
			$map['cate_id'] = $cate_id;
		}
		$map['is_show'] = 1;
		if($num == 1){
			$list = $t_cms_ad->where($map)->limit($num)->order("rand()")->select();
		}else{
			$list = $t_cms_ad->where($map)->limit($num)->order("`is_order` Asc,`id` Asc")->select();
		}
		foreach($list as $key => $val){
			$list[$key]['cover'] = OssFileService::getOssUrl(($val['cover']));
		}
		return $list ? $list : [];
	}

	/**
	 * 获取轮播图
	 *
	 * @param int   $cate_id
	 * @param int   $num
	 * @param array $map
	 * @return null
	 */
	public static function getAdLimitList($cate_id = 0, $start = 0, $end = 8){
		$t_cms_ad = M("cms_ad");
		if($cate_id > 0){
			$map['cate_id'] = $cate_id;
		}
		$map['is_show'] = 1;
		$list = $t_cms_ad->where($map)->limit($start, $end)->order("`is_order` Asc,`id` Asc")->select();
		foreach($list as $key => $val){
			$list[$key]['cover'] = get_cover_img($val['cover']);
		}
		return $list ? $list : [];
	}

	/**
	 * 获取文章列表
	 *
	 * @param array  $where
	 * @param string $order
	 * @param int    $pagesize
	 * @param int    $pagenum
	 * @return array
	 */
	public static function getArticleList($where = [], $order = '', $pagesize = 1, $pagenum = 10){
		$t_cms_article = M('user_cms_article');
		$startnum = ($pagesize - 1) * $pagenum;
		if(empty($order)) $order = 'id Desc';
		$where['is_delete'] = 0;
		$data_list = $t_cms_article->where($where)->order($order)->limit($startnum, $pagenum)->select();
		foreach($data_list as $key => $val){
			$cate_info = CmsData::getCateInfo($val['cate_id']);
			$data_list[$key]['cate_name'] = $cate_info['cate_name'];
		}
		return $data_list ? $data_list : [];
	}

	/**
	 * 获取文章详情
	 *
	 * @param array  $where
	 * @param string $order
	 * @param int    $pagesize
	 * @param int    $pagenum
	 * @return array
	 */
	public static function getArticleInfo($article_id){
		$t_cms_article = M('user_cms_article');
		$where['id'] = $article_id;
		$data_info = $t_cms_article->where($where)->find();
        if(!$data_info){
            return [];
        }
		$data_info['content'] = $data_info['content'] ? json_decode($data_info['content'], true) : [];
        //过滤文章内容
        $data_info['content'] = self::filterArticleContent($data_info['content']);
		$cate_info = CmsData::getCateInfo($data_info['cate_id']);
		$data_info['cate_name'] = $cate_info['cate_name'];
		return $data_info;
	}
    private static function filterArticleContent($content = []){
        $newContent = [];
        foreach ($content as $key => $val){
            $_content = arr_get($val, 'content', '');
            if(!$_content){
                continue;
            }
            $newContent [] = $val;
        }
        return $newContent;
    }

	/**
	 * 获取单页
	 *
	 * @param $page_type
	 * @return mixed
	 */
	public static function getPageInfo($page_id = 0, $field = ''){
		$t_cms_page = M("cms_page");
		$map['id'] = intval($page_id);
		$info = $t_cms_page->where($map)->find();
		if(!empty($field)) return $info[$field];
		return $info;
	}

	/**
	 * 分类详情
	 *
	 * @param int $cate_id
	 * @return mixed|string
	 */
	public static function getCateInfo($cate_id = 0){
		$info = M('user_cms_category')->where(['id' => $cate_id])->find();
		return $info ? $info : '';
	}

	/**
	 * 分类详情
	 *
	 * @param int $cate_id
	 * @return mixed|string
	 */
	public static function getCateList($where = []){
		$where['cate_status'] = 1;
		$info = M('user_cms_category')->where($where)->order("cate_sort asc,id desc")->select();
		return $info ? $info : '';
	}

	//读取当前分类下的所有分类
	public static function getCateChirlenList($id){
		if(checkSqlID($id) == false){
			return 0;
		}
		$cate_str = $id;
		$t_cms_category = M("cms_category");
		if($cate_info = $t_cms_category->where("id=$id")->find()){
			if($cate_info['pid'] == 0){
				$two_cate_list = $t_cms_category->where("pid=$id")->select();
				if($two_cate_list){
					foreach($two_cate_list as $key => $val){
						$cate_str .= ','.$val['id'];
					}
				}
			}
		}
		return $cate_str;
	}
}
